<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
</body>
</html>
<?php 

class file_model {

	/*
		
	*/
	
	function __construct(){
		
		
	}
	
	function upload_file($file,$file_tmp){
		//检查
		$dir='../ui/upload/';
		if(!file_exists($dir.$file)){
			move_uploaded_file($file_tmp,$dir.$file);
			echo '上传成功！';
		}else{
		echo '存在同名文件，请修改后在上传！谢谢';
		}
	}
	
	function excel_input($file,$filetempname){
    //自己设置的上传文件存放路径
    $filePath = '../ui/upload/';
    $str = "";   
    //下面的路径按照你PHPExcel的路径来修改
    require_once 'classes/PHPExcel.php';
    require_once 'classes/PHPExcel/IOFactory.php';
    require_once 'classes/PHPExcel/Reader/Excel5.php';

    //注意设置时区
	date_default_timezone_set("UTC"); 
    $time=date("y-m-d-H-i-s");//去当前上传的时间 
    //获取上传文件的扩展名
    $extend=strrchr ($file,'.');
    //上传后的文件名
    $name=$time.$extend;
    $uploadfile=$filePath.$name;//上传后的文件名地址 
    //move_uploaded_file() 函数将上传的文件移动到新位置。若成功，则返回 true，否则返回 false。
    $result=move_uploaded_file($filetempname,$uploadfile);//假如上传到当前目录下
    //echo $result;
    if($result){ //如果上传文件成功，就执行导入excel操作
        $objReader = PHPExcel_IOFactory::createReader('Excel5');//use excel2007 for 2007 format 
        $objPHPExcel = $objReader->load($uploadfile); 
		
		$objWorksheet = $objPHPExcel->getActiveSheet();
        $highestRow = $objWorksheet->getHighestRow(); 
        echo '有效总行数='.$highestRow;
        echo "<br>";
        $highestColumn = $objWorksheet->getHighestColumn();
        $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);//总列数
        echo '有效总列数='.$highestColumnIndex;
        echo "<br>";
        $headtitle=array(); 
	
		$dom=new DOMDocument('1.0','utf-8');
		$path="../ui/upload/xml/".date('h-m-s').".xml";     //  $path 为xml文件的存储路径。
		
		//root node
		$sheet=$dom->createElement('sheet');  
		$dom->appendChild($sheet);
		
		for($col = 0;$col < $highestColumnIndex;$col++){
		//一级
		$cell_1[$col]=$dom->createElement('C'.$col);
		$sheet->appendChild($cell_1[$col]);
		 
		
		//$rol=$dom->createTextNode('');
		for ($row = 1;$row <= $highestRow;$row++){
		//二级
		$strs[$row] =$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
				
		$cell_2[$row]=$dom->createElement('R'.$row); 		
		$cell_2_value[$row]=$dom->createTextNode($strs[$row]);
		$cell_2[$row]->appendChild($cell_2_value[$row]);
		$cell_1[$col]->appendChild($cell_2[$row]);
		
		}
		}
		
		echo $dom->saveXML();
		$dom->save($path);	
	}

		
	}
	
	function excel_export($filepath_source,$filename){
	require_once 'Classes/PHPExcel.php';

	/** PHPExcel_Writer_Excel2007  */
	include 'Classes/PHPExcel/Writer/Excel5.php';
	$objPHPExcel = new PHPExcel();
	$objPHPExcel->setActiveSheetIndex(0);
	
	//选择要导出的内容xml文件
	$file=$filepath_source;
	
	$xml = simplexml_load_file($file);
	
	//假设生成的表格最大的长和宽
	$word=array('A','B','C','D','E','F','G','H','I','J','K','L');
	$num=array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31','32','33','34','35','36','37','38','39','40','41','42','43','44','45','46','47','48','49','50','51','52','53','54','55','56','57','58','59','60','61','62','63','64','65','66','67','68','69','70','71','72','73','74','75','76','77','78','79','80','81','82','83','84','85','86','87','88','89','90','91','92','93','94','95','96','97','98','99','100');
	$i=-1;
	
	foreach($xml->children() as $childs){
		//宽--字母栏
		
		$i++;
		$j=0;
		foreach($childs as $child){
		//长--数字栏
	
		echo "编号：".$word[$i].$num[$j++]."---".$child;
		$objPHPExcel->getActiveSheet()->setCellValue($word[$i].$num[$j],$child);		
		}
	}
	
	$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
	
	$filepath_dest='../ui/upload/xml/';
	$file_name=$filepath_dest.$filename.".xls";
	if(!file_exists($file_name)){
	$objWriter->save($file_name);
	}else{
		echo '文件名相同！';
	}

	//  echo $xml->asXML();

	}

}

$file=new file_model();
?>
